package com.kaibes.platform.file.client.admin;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.kaibes.core.base.option.NullOptions;
import com.kaibes.orm.core.core.Page;
import com.kaibes.platform.file.client.db.FileMap;
import com.kaibes.platform.file.client.db.FileMapService;
import com.kaibes.platform.file.client.db.FileServer;
import com.kaibes.platform.file.client.db.FileServerService;
import com.kaibes.platform.file.client.util.FileServerUtils;
import com.kaibes.web.thymeleaf.ThymeleafConfig;

@Component
public class FileClientUtils implements InitializingBean {

	@Autowired
	private FileServerService fileServerService;
	@Autowired
	private FileServerUtils fileServerUtils;
    @Autowired
    private FileMapService fileMapService;
    @Autowired
    private ThymeleafConfig webConfig;
    
    public Page<FileMap> page(int pageIndex, int pageSize) {
        return fileMapService.page(pageIndex, pageSize);
    }
    
    public Page<FileMap> pageByTag(int pageIndex, int pageSize, String tag) {
        return fileMapService.pageByTag(pageIndex, pageSize, tag);
    }
    
    public String getHost(Integer id) {
		return NullOptions.returnString(fileServerService.getById(id), server->{
			return server.getHost();
		});
	}
    
    public boolean test(Integer id) {
    	FileServer server = fileServerService.getById(id);
    	if (server != null) {
    		return fileServerUtils.test(server);
    	}
    	return false;
    }
    
	@Override
	public void afterPropertiesSet() throws Exception {
		webConfig.putStaticVariable("fileClientUtils", this);
	}
    
}
